//
//  NSCalendar+BKODCalendarExtensions.m
//  CanlendarTest
//
//  Created by yons on 17/2/14.
//  Copyright © 2017年 马乾征. All rights reserved.
//

#import "NSCalendar+BKODCalendarExtensions.h"

@implementation NSCalendar (BKODCalendarExtensions)
- (NSDate *)od_firstDayOfMonth:(NSDate *)month
{
    if (!month) return nil;
    NSDateComponents *components = [self components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth fromDate:month];
    components.day = 1;
    return [self dateFromComponents:components];
}

- (NSDate *)od_lastDayOfMonth:(NSDate *)month
{
    if (!month) return nil;
    NSDateComponents *components = [self components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth fromDate:month];
    components.month++;
    components.day = 0;
    return [self dateFromComponents:components];
}

- (NSInteger)od_numberOfDaysInMonth:(NSDate *)month
{
    NSRange range = [self rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:month];
    return range.length;
}

- (NSDateComponents *)od_dateComponents:(NSDate *)date
{
    NSDateComponents *dateComponents = [self components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitWeekOfMonth fromDate:date];
    return dateComponents;
}

- (NSInteger)od_numberOfDaysFromDate:(NSDate *)startDate toDate:(NSDate *)endDate
{
    //去掉时分秒信息
    NSDate *fromDate;
    NSDate *toDate;
    [self rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:startDate];
    [self rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:endDate];
    
    NSDateComponents *dateComponents = [self components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:NSCalendarMatchStrictly];
    return dateComponents.day;
}

- (NSInteger)od_numberOfMonthFromMonth:(NSDate *)startMonth toMonth:(NSDate *)endMonth{
    
    NSDate *fromDate;
    NSDate *toDate;
    [self rangeOfUnit:NSCalendarUnitMonth startDate:&fromDate interval:NULL forDate:startMonth];
    [self rangeOfUnit:NSCalendarUnitMonth startDate:&toDate interval:NULL forDate:endMonth];
    
    NSDateComponents *dateComponents = [self components:NSCalendarUnitMonth fromDate:fromDate toDate:toDate options:NSCalendarMatchStrictly];
    return dateComponents.month;
}

- (NSDate *)od_dateWithDifferenceDays:(NSInteger)days atDate:(NSDate *)date
{
    NSDateComponents *someDate = [[NSDateComponents alloc] init];
    [someDate setYear:0];
    [someDate setMonth:0];
    [someDate setDay:days];
    
    NSDate *resultDate = [self dateByAddingComponents:someDate toDate:date options:NSCalendarMatchStrictly];
    return resultDate;
}

- (NSDate *)od_dateWithDifferenceMonth:(NSInteger)month atDate:(NSDate *)date
{
    NSDateComponents *someDate = [[NSDateComponents alloc] init];
    [someDate setYear:0];
    [someDate setMonth:month];
    [someDate setDay:0];
    
    NSDate *resultDate = [self dateByAddingComponents:someDate toDate:date options:NSCalendarMatchStrictly];
    return resultDate;
}
@end
